<?php
/**
 * 单件实例化 p 
 *
 * @filesource _db.class.php
 * @package peck
 * @subpackage _db
 * @version $id: 0.3, utf8, Sun Dec 20 21:25:28 CST 2009
 * @author LD King <kldscs[at]gmail.com>
 * @copyright Copyleft (D.) 2007 - 2010 MiFunny China Inc.
 * @link http://mifunny.info/
 * @example 
 * 
 *   $_config['MagicDB'] = array(
 *   	'driver' => 'mysql',  //数据库驱动, 参看 http://www.php.net/manual/en/pdo.drivers.php
 *   	'dsn' => 'mysql:dbname=testdb;host=127.0.0.1;port=3333',
 *   	'username' => 'test', 
 *   	'password' => 'test',
 *   	'charset' => 'utf8',
 *   	'collate' => 'utf8_general_ci',
 *   	'persistent' => true,  //是否使用长连接
 *   	'options' => array(
 *   		PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
 *   		), //额外选项
 *     );
 * 
 *  如果是MySQL, dsn包括 :
 *   	'host' => '127.0.0.1',  //MySQL服务器IP地址
 *   	'port' => '3306',  //端口
 *   	'unix_socket' => '/var/mysql//mysql.sock', 
 *   	'dbname' => 'test', //数据库名
 */

class _db {
	private static $instance;
	
	/**
	 * 数据库对象 (默认PDO)
	 * @var pdo object
	 */
	private $_db;
	
	private function __construct(){}
	private function __clone(){}
	
	/**
	 * 获得静态对象实例
	 * 
	 * @return self
	 */
	private static function instance(){
		if( !(self::$instance instanceof self) ) {
			$policy = _core::object()->get('database');
			if ( empty($policy) ) {
        		throw new LogicException('('.__METHOD__.')'.
        		  "Error: Someone should give any stuff to _core::object()->set('database')!");
        	}
			self::$instance = new self();
			//Windows下使用MagicDB, Win办的PDO简直是个鸡肋！
			if ( extension_loaded('pdo_'.$policy['driver']) ){
				if( $policy['driver']=='mysql' and stripos(PHP_OS, 'win') === 0 ){
					self::$instance->_db = self::_initMySQL($policy); //MagicDB
				}else
					self::$instance->_db = new _pdo($policy);
        	}elseif( $policy['driver']=='mysql' ){
				self::$instance->_db = self::_initMySQL($policy); //MagicDB
        	}else{
        		throw new LogicException('('.__METHOD__.')'.
        			"Error: pdo_{$policy['driver']} initialization failed, check your system configuration !");
        	}
		}
		return self::$instance;
	}//END func getInstance
	
	public static function object(){
		return self::instance()->_db;
	}
	
	/**
	 * 初始化非PDO连接对象
	 *
	 * @param array $policy 配置数组
	 * 
	 * @return database object
	 */
	private static function _initMySQL($policy){
		preg_match("/host=(.*?)($| |;)/i",$policy['dsn'],$host);
		preg_match("/port=(.*?)($| |;)/i",$policy['dsn'],$port);
		preg_match("/unix_socket=(.*?)($| |;)/i",$policy['dsn'],$unix_socket);
		preg_match("/dbname=(.*?)($| |;)/i",$policy['dsn'],$dbname);
		$policy['host'] = isset($host[1]) ? $host[1] : NULL;
		$policy['port'] = isset($port[1]) ? $port[1] : NULL;
		$policy['unix_socket'] = isset($unix_socket[1]) ? $unix_socket[1] : NULL;
		$policy['dbname'] = isset($dbname[1]) ? $dbname[1] : NULL; 

		//start MagicDB
		if( extension_loaded('mysqli') ){
        	return new _mysqli($policy);
        }else {
        	return new _mysql($policy);
        }
	}//END func _initializeMagicDB
	
}//END class DB
?>
